use "${data}IndirectSurveyDataReplication.dta", clear


*********************** DO HOUSEHOLD AND LEADER INDICES RANK THE SAME HOUSEHOLD AS POOREST *************************

bysort ejnclnmid: egen leaderrank_IPW = rank(IPW_leader)
bysort ejnclnmid: egen hhrank_IPW = rank(IPW_HH)
gen poorest_hh_IPW = hhrank_IPW == 1
replace poorest_hh_IPW = . if hhrank_IPW == .
gen poorest_leader_IPW = leaderrank_IPW == 1
replace poorest_leader_IPW = . if leaderrank_IPW == .

bysort ejnclnmid: egen leaderrank_PIB = rank(PIB_leader)
bysort ejnclnmid: egen hhrank_PIB = rank(PIB_HH)
gen poorest_hh_PIB = hhrank_PIB == 1
replace poorest_hh_PIB = . if hhrank_PIB == .
gen poorest_leader_PIB = leaderrank_PIB == 1
replace poorest_leader_PIB = . if leaderrank_PIB == .

bysort ejnclnmid: egen leaderrank_PCA = rank(PCA_leader)
bysort ejnclnmid: egen hhrank_PCA = rank(PCA_HH)
gen poorest_hh_PCA = hhrank_PCA == 1
replace poorest_hh_PCA = . if hhrank_PCA == .
gen poorest_leader_PCA = leaderrank_PCA == 1
replace poorest_leader_PCA = . if leaderrank_PCA == .

label define poorest 0 "Wealthier" 1 "Poorest", replace

label values poorest_leader_PIB poorest_hh_PIB poorest_leader_IPW poorest_hh_IPW poorest_leader_PCA poorest_hh_PCA poorest

estpost tab poorest_leader_PIB poorest_hh_PIB
esttab . using "${outputs}rank.tex", replace cell(pct(fmt(%5.1f))) unstack nonumber nomtitles nodepvars nonumbers ///
 label fragment collabels("" "" "" )  noobs ///
  prehead( & \multicolumn{3}{c}{Household responses} \\ ) ///
  posthead(Leader responses & \\ \hline \multicolumn{4}{l}{Simple index} \\ ) ///
  postfoot(\hline) 

estpost tab poorest_leader_IPW poorest_hh_IPW
esttab .  using "${outputs}rank.tex", append cell(pct(fmt(%5.1f))) unstack nonumber nomtitles nodepvars nonumbers ///
 label fragment  collabels( "" "") noobs ///
 posthead(\hline \multicolumn{4}{l}{IPW index} \\ ) ///
 postfoot(\hline) 
 
  
estpost tab poorest_leader_PCA poorest_hh_PCA
esttab .  using "${outputs}rank.tex", append cell(pct(fmt(%5.1f))) unstack nonumber nomtitles nodepvars nonumbers ///
 label fragment  collabels( "" "") noobs ///
 posthead(\hline \multicolumn{4}{l}{PCA index} \\ ) ///
 postfoot( \hline \hline \end{tabular} \begin{tablenotes}[para,flushleft] \footnotesize{Cells show percentages in each category.  The ///
	top panel shows poorest household identified using the IPW index and  the bottom panel using the simple  ///
	sum of binary indicators. In both cases, the poorest is indicated by the poorest household out of the three in the community sample.} \end{tablenotes} )

*************** COMPARE SIMPLE TO MORE COMPLEX HH INDICES **********************
bysort ejnclnmid: egen hhrank_IPW_full = rank(assets_invprop)
la var hhrank_IPW_full "HH poverty rank, full IPW index"

bysort ejnclnmid: egen hhrank_bin_full = rank(assets_simple)
la var hhrank_bin_full "HH poverty rank, full simple index"

bysort ejnclnmid: egen hhrank_PCA_full = rank(assets_pca)
la var hhrank_PCA_full "HH poverty rank, full PCA index"



estimates clear
foreach x in bin IPW PCA {
reg hhrank_`x' hhrank_`x'_full, nocons cluster(ejnclnmid)
estimates store `x'1
xtreg hhrank_`x' hhrank_`x'_full, fe i(ejnclnmid)
estimates store `x'2

}

esttab bin1 IPW1 PCA1 bin2 IPW2 PCA2  ///
	using "${outputs}pov_robust.tex",  ///
	se nonotes  style(tex)  b(%12.3f) se(%12.3f)  ///
	starlevels(* 0.10 ** 0.05 *** 0.01) label mlabels("" "" "" "" "" "" ) ///
	 keep(hhrank*) nonumbers  replace  ///
	 prehead({ \begin{tabular}{lcccccc}    ///
	 \hline \hline ///
	 & OLS & OLS & OLS &  FE  & FE & FE \\  & (1) & (2) & (3) & (4) & (5) & (6)\\   ) ///
	 postfoot( \hline \hline \end{tabular} } \begin{tablenotes}[para,flushleft] \footnotesize{Estimations regress the ranking of the parsimonious IPW and binary indices on  ///
	the ranking of households as calculated using the full set of household responses. OLS estimates have no controls and no constant. Fixed  ///
	effects estimates use ejido-level fixed effects.  In both cases the standard errors are clustered at ///
	the level of the ejido.} \end{tablenotes} )

	
twoway (lpolyci IPW_HH assets_invprop, fcolor(none) clcolor(ekblue)), graphregion(color(white)) ytitle("Parsimonious HH IPW index") xtitle("Full household IPW index") ///
legend(cols(1) label(1 "95% CIs") label(2 "Kernel regression"))  yscale(range(0 .50 2))
graph 	save "${outputs}corr_robustipw.gph", replace

twoway (lpolyci PCA_HH assets_pca, fcolor(none) clcolor(ekblue)), graphregion(color(white)) ytitle("Parsimonious HH PCA index") xtitle("Full household PCA index") ///
legend(cols(1) label(1 "95% CIs") label(2 "Kernel regression"))  yscale(range(0 .50 2))
graph 	save "${outputs}corr_robustpca.gph", replace


twoway (lpolyci PIB_HH assets_simple, fcolor(none) clcolor(ekblue)), graphregion(color(white)) ytitle("Parsimonious HH binary index") xtitle("Full household binary index") ///
legend(cols(1) label(1 "95% CIs") label(2 "Kernel regression")) 
graph 	save "${outputs}corr_robustbin.gph", replace

gr combine 	"${outputs}corr_robustbin.gph" "${outputs}corr_robustipw.gph" "${outputs}corr_robustpca.gph", ///
			graphregion(color(white))
gr export "${outputs}poverty_robust.eps", replace

********* COMPARE CLASSIFICATION USING COMPLEX VERSUS SIMPLE INDEX ***

egen median_IPW = median(assets_invprop)
egen median_binary = median(assets_simple)
egen median_pca = median(assets_pca)

gen poor_IPW = assets_invprop < median_IPW
replace poor_IPW = . if assets_invprop == .

gen poor_binary = assets_simple < median_binary
replace poor_binary = .  if assets_simple == .

gen poor_pca= assets_pca < median_pca
replace poor_pca = .  if assets_pca == .


label values poor_binary poor_IPW poor_pca poor



estpost tab poor_binary poor_binary_HH
esttab . using "${outputs}error_pov_full.tex", replace cell(pct(fmt(%5.1f))) unstack nonumber nomtitles nodepvars nonumbers ///
 label fragment collabels("" "" "" )  noobs ///
  prehead( & \multicolumn{3}{c}{Full index} \\ ) ///
  posthead(Parsimonious& \\ \hline \multicolumn{4}{l}{Simple index} \\ ) ///
  postfoot(\hline) 

estpost tab poor_pca poor_IPW_HH
esttab .  using "${outputs}error_pov_full.tex", append cell(pct(fmt(%5.1f))) unstack nonumber nomtitles nodepvars nonumbers ///
 label fragment  collabels( "" "") noobs ///
 posthead(\hline \multicolumn{4}{l}{IPW index} \\ ) ///
postfoot(\hline) 
 
  
estpost tab poor_pca poor_PCA_HH
esttab .  using "${outputs}error_pov_full.tex", append cell(pct(fmt(%5.1f))) unstack nonumber nomtitles nodepvars nonumbers ///
 label fragment  collabels( "" "") noobs ///
 posthead(\hline \multicolumn{4}{l}{PCA index} \\ ) ///
 postfoot( \hline \hline \end{tabular} \begin{tablenotes}[para,flushleft] \footnotesize{Cells show percentages in each category.  The ///
	top panel shows poverty classifications using the IPW index and  the bottom panel using the simple  ///
	sum of binary indicators. In both cases, the poor are those with less than the median level of the index ///
	calculated across the entire sample.} \end{tablenotes} )




